package com.kelvin.paopao.module.main.My;

import android.content.Intent;

import com.hyphenate.EMCallBack;
import com.kelvin.kelvinlib.utils.ToastUtil;
import com.kelvin.paopao.easemob.EaseMobHelper;
import com.kelvin.paopao.module.main.MainActivity;
import com.kelvin.paopao.utils.Constant;
import com.kelvin.paopao.utils.DialogMaker;
import com.hyphenate.easeui.utils.SpUtil;

import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Action;
import io.reactivex.functions.Consumer;

/**
 * Created by MCK on 2017/5/9.
 */

public class MainMyPresenter implements IMainMyPresenter {

    private MainMyFragment mView;

    public MainMyPresenter(MainMyFragment view) {
        this.mView = view;
    }

    @Override
    public void logout() {
        Observable.create(new ObservableOnSubscribe<String>() {
            @Override
            public void subscribe(@NonNull ObservableEmitter<String> e) throws Exception {
                e.onNext(SpUtil.getLoginHuanxin_id(mView.getContext()));
            }
        }).doOnSubscribe(new Consumer<Disposable>() {
            @Override
            public void accept(@NonNull Disposable disposable) throws Exception {
                DialogMaker.showProgressDialog(mView.getContext(), "正在退出~~");
            }
        }).delay(3000, TimeUnit.MILLISECONDS).subscribe(new Consumer<String>() {
            @Override
            public void accept(@NonNull String s) throws Exception {
                EaseMobHelper.getInstance().logout(false, new EMCallBack() {
                    @Override
                    public void onSuccess() {
                        DialogMaker.diamiss();
                        ToastUtil.showToastOnThread(mView.getContext(), "退出成功");

                        Intent intent = new Intent(mView.getContext(), MainActivity.class);
                        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                        mView.getContext().startActivity(intent);

                        SpUtil.setLoginphone(mView.getContext(), null);
                        SpUtil.setLoginUserName(mView.getContext(), null);
                        SpUtil.setLoginAvatar(mView.getContext(), null);
                        SpUtil.setLoginHuanxin_id(mView.getContext(), null);

                        Constant.IS_LOGIN = false;
                    }

                    @Override
                    public void onError(int i, String s) {
                        DialogMaker.diamiss();
                        ToastUtil.showToastOnThread(mView.getContext(), "退出失败");
                    }

                    @Override
                    public void onProgress(int i, String s) {

                    }
                });
            }
        }, new Consumer<Throwable>() {
            @Override
            public void accept(@NonNull Throwable throwable) throws Exception {
                DialogMaker.diamiss();
            }
        }, new Action() {
            @Override
            public void run() throws Exception {
                DialogMaker.diamiss();
            }
        });
    }

    @Override
    public void getData(boolean isRefresh) {

    }

    @Override
    public void getMoreData() {

    }
}
